{% extends 'base.html' %}

{% block title %}创建角色{% endblock %}

{% block body %}
    <form class="layui-form layui-form-pane" action="">
        {% csrf_token %}
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" required lay-verify="required" placeholder="输入角色名"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item ">
            <label class="layui-form-label">许可选择</label>
            <div class="xm-select-demo layui-input-inline" id="select-role"></div>
        </div>

        <div class="layui-form-item" style="margin-top: 1%;">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="createRole">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
{% endblock %}
{% block js %}
    <script>
        layui.use(['jquery', 'form', 'layer', 'tree'], function () {
            var form = layui.form
                , $ = layui.jquery
                , layer = layui.layer
                , tree = layui.tree;


            $.ajax({
                url: "{% url 'auth:permission_api' %}?role=true",
                type: 'GET',
                dataType: 'json',
                success: function (res) {
                    {#console.log(res.data)#}
                    data = res.data

                    tree.render({
                        elem: '#select-role',
                        data: data,
                        showCheckbox: true,  // 是否显示复选框
                        id: 'demo-id-1',
                        isJump: true, // 是否允许点击节点时弹出新窗口跳转
                        click: function (obj) {
                            var data = obj.data;  //获取当前点击的节点数据
                            layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                        }
                    });
                }
            })

            form.on('submit(createRole)', function (data) {
                data = data.field;
                data['csrfmiddlewaretoken'] = $('[name="csrfmiddlewaretoken"]').val();
                $.ajax({
                    url: "{% url 'auth:role_api' %}",
                    type: 'POST',
                    data: data,
                    dataType: 'json',
                    success: function (res) {
                        if (res.code === 0) {
                            layer.msg(res.msg, {icon: 6});
                            document.location.href = "{% url 'auth:role' %}", true;
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (error) {
                        layer.msg(error.msg, {icon: 5});
                    }
                })
                return false;
            });

        });
    </script>
{% endblock %}